/**
 *
 * (c) Copyright Ascensio System SIA 2021
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.shinewing.sconlyoffice.helpers;

import com.shinewing.sconlyoffice.context.SpringContextUtils;
import com.shinewing.sconlyoffice.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


public class CookieManager {
    private HashMap<String, String> cookiesMap;


    public CookieManager(HttpServletRequest request) throws UnsupportedEncodingException {
        UserDao userDao = SpringContextUtils.getBean(UserDao.class);
        cookiesMap = new HashMap<String, String>();
        String uid = request.getParameter("uid");
        if(!Objects.isNull(uid)){
            Map<String, Object> user = userDao.findByUid(uid);
            if (user != null) {
                cookiesMap.put("ulang", "zh-CN");
                cookiesMap.put("uname", Objects.toString(user.get("SSUSER_USERNAME")));
                cookiesMap.put("uid", uid);
            }
        } else {
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    cookiesMap.putIfAbsent(cookie.getName(), URLDecoder.decode(cookie.getValue(), "UTF-8"));
                }
            }
        }
    }

    public String getCookie(String name) {
        return cookiesMap.get(name);
    }
}
